﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode.BusinessLayer;
using QRCode.BusinessLayer.General;
using QRCode.BusinessEntities;
using QRCode.ViewModel;

namespace QRCode.Web.Controllers
{
    [UserAuthorize]
    public class AuthenticationController : Controller
    {
        private UserManager userManager = new UserManager();
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Login(LoginViewModel loginViewModel)
        {
            if (ModelState.IsValid)
            {
                string _passowrd = Security.SHA256(loginViewModel.Password);
                var _response = userManager.Verify(loginViewModel.Accounts,_passowrd);
                if (_response.Code == 1)
                {
                    var _user = userManager.Find(loginViewModel.Accounts);
                    Session.Add("UserID", _user.UserID);
                    Session.Add("Username", _user.Username);
                    _user.LastLoginTime = DateTime.Now;
                    _user.LastLoginIP = Request.UserHostAddress;
                    userManager.Update(_user);
                    return RedirectToAction("Index", "Scan");
                }
                else if (_response.Code == 2) ModelState.AddModelError("Accounts", _response.Message);
                else if (_response.Code == 3) ModelState.AddModelError("Password", _response.Message);
                else ModelState.AddModelError("", _response.Message);
            }
            return View(loginViewModel);
        }
        public ActionResult UserInfo()
        {
            var _user = userManager.Find(Session["Username"].ToString());
            UserInfoViewModel uInfo = new UserInfoViewModel();
            uInfo.RoleID = _user.RoleID;
            uInfo.Name = _user.Name;
            uInfo.Password = "******";
            uInfo.Username = _user.Username;
            uInfo.Email = _user.Email;
            return View("UserInfo",uInfo);
        }
        public PartialViewResult ChangePwdPartialView()
        {
            //UploadPwdViewModel pwd = new UploadPwdViewModel();
            return PartialView("UplPwd");
        }
        [HttpPost]
        public JsonResult ChangeNamePartialView(string name)
        {
            Response _resp = new Response();
            if (name != "")
            {
                _resp = userManager.ChangeName(int.Parse(Session["UserID"].ToString()), name);
                if (_resp.Code == 1) _resp.Message = "昵名已修改";
            }
            else
            {
                _resp.Message = "请输入昵名";
            }
            return Json(_resp);
        }
        /// <summary>
        /// 添加【Json】
        /// </summary>
        /// <param name="addAdmin"></param>
        /// <returns></returns>
        [ValidateAntiForgeryToken]
        [HttpPost]
        [ActionName("ChangePwdPartialView")]
        public JsonResult UplPwdJson(UploadPwdViewModel pwd)
        {
            Response _resp = new Response();
            if (pwd.Password == pwd.ConfirmPassword & pwd.Password != "")
            {
                _resp = userManager.ChangePassword(int.Parse(Session["UserID"].ToString()), Security.SHA256(pwd.Password));
                if (_resp.Code == 1) _resp.Message = "密码设置成功";
            }
            else
            {
                _resp.Message = "输入密码不一致。";
            }
            return Json(_resp);
        }

    }
}